package com.imooc.coupon.entity;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.imooc.coupon.constant.CouponCategory;
import com.imooc.coupon.constant.DistributeTarget;
import com.imooc.coupon.constant.ProductLine;
import com.imooc.coupon.converter.CouponCategoryConverter;
import com.imooc.coupon.converter.DistributeTargetConverter;
import com.imooc.coupon.converter.ProductLineConverter;
import com.imooc.coupon.converter.RuleConvert;
import com.imooc.coupon.serialization.CouponTemplateSerialize;
import com.imooc.coupon.vo.TemplateRule;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.lang.annotation.Target;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 优惠券模板实体类定义：基础属性和规则属性
 *
 * @description:
 * @author：naxiaozi，微信：naxiaozi
 * @date: 2025/8/12
 * @Copyright：
 * @EntityListeners 实体监听 自动添加创建时间和更新时间字段
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "coupon_template")
@JsonSerialize(using = CouponTemplateSerialize.class)
public class CouponTemplate implements Serializable {
    //自增主键ID
    @Id
    //自增
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    //字段名称 nullable 不可为null
    @Column(name = "id", nullable = false)
    @Basic //数据数据表的列 和数据库一致 默认的，可加可不加
    /**
     *  @Transient 非数据库字段，不返回
     */
    private Integer id;
    /**
     * 是否为可用状态
     */
    @Column(name = "available", nullable = false)
    private Boolean available;
    /**
     * 是否过期
     */
    @Column(name = "expired", nullable = false)
    private Boolean expired;
    /**
     * 优惠券名称
     */
    @Column(name = "name",nullable = false)
    private String name;
    /**
     * 优惠券logo
     */
    @Column(name = "logo",nullable = false)
    private String logo;
    /**
     * 优惠券描述信息
     */
    @Column(name = "intro",nullable = false)
    private String desc;

    /**
     * 优惠券分类
     */
    @Convert(converter = CouponCategoryConverter.class)
    @Column(name = "category",nullable = false)
    private CouponCategory category;

    /**
     * 产品线
     */
    @Convert(converter = ProductLineConverter.class)
    @Column(name = "product_line",nullable = false)
    private ProductLine productLine;

    /**
     * 优惠券数量
     */
    @Column(name = "coupon_count",nullable = false)
    private Integer count;
    /**
     * 创建时间 @CreatedDate 自动填充创建时间
     */
    @CreatedDate
    @Column(name = "create_time",nullable = false)
    private Date createTime;

    /**
     * 创建用户
     */
    @Column(name = "user_id",nullable = false)
    private Long userId;

    /**
     * 优惠券模板编码
     */
    @Column(name = "template_key",nullable = false)
    private String key;
    /**
     * 目标用户
     */
    @Convert(converter = DistributeTargetConverter.class)
    @Column(name = "target",nullable = false)
    private DistributeTarget target;
    /**
     * 优惠券规则
     */
    @Column(name = "rule",nullable = false)
    @Convert(converter = RuleConvert.class)
    private TemplateRule rule;
    public CouponTemplate(String name,String logo,String desc,String category,
                          Integer productLine,Integer count,Long userId,
                          Integer target,TemplateRule rule){
        this.available= false;
        this.expired = false;
        this.name=name;
        this.logo=logo;
        this.desc = desc;
        this.category=CouponCategory.of(category);
        this.productLine= ProductLine.of(productLine);
        this.count= count;
        this.userId = userId;
        this.key = productLine.toString()+category+new SimpleDateFormat("yyyyMMdd").format(new Date());
        this.target = DistributeTarget.of(target);
        this.rule = rule;
    }
}
